﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

/**
 * 创建时间：2014/11/5 17:11:10
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace CUWo.Common
{

    public sealed class Helper
    {
        /// <summary>
        /// 生成随机因子
        /// <para>循环中生成随机数时必用</para>
        /// </summary>
        /// <returns></returns>
        public int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        /// <summary>
        /// 获取指定字节长度的中英文混合字符串
        /// </summary>
        public string GetString(string str, int len)
        {
            string result = string.Empty;// 最终返回的结果
            int byteLen = Encoding.UTF8.GetByteCount(str);// 单字节字符长度
            int charLen = str.Length;// 把字符平等对待时的字符串长度
            int byteCount = 0;// 记录读取进度
            int pos = 0;// 记录截取位置
            if (byteLen > len)
            {
                for (int i = 0; i < charLen; i++)
                {
                    if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2
                        byteCount += 2;
                    else// 按英文字符计算加1
                        byteCount += 1;
                    if (byteCount > len)// 超出时只记下上一个有效位置
                    {
                        pos = i;
                        break;
                    }
                    else if (byteCount == len)// 记下当前位置
                    {
                        pos = i + 1;
                        break;
                    }
                }

                if (pos > 0)
                    result = str.Substring(0, pos);
                else
                    result = str;
            }
            else
                result = str;

            return result;
        }


        /// <summary>
        /// 16进制转argb颜色
        /// <para>#c1dc23写成#ffc1dc23，其中前两位ff是透明度</para>
        /// </summary>
        /// <param name="colorName"></param>
        /// <returns></returns>
        public static Color HexToColor(string colorName)
        {
            if (colorName.StartsWith("#"))
                colorName = colorName.Replace("#", string.Empty);
            int v = int.Parse(colorName, System.Globalization.NumberStyles.HexNumber);
            return new Color()
            {
                A = Convert.ToByte((v >> 24) & 255),
                R = Convert.ToByte((v >> 16) & 255),
                G = Convert.ToByte((v >> 8) & 255),
                B = Convert.ToByte((v >> 0) & 255)
            };
        }






        public static double GetMinute(TimeSpan strTime)
        {
            return strTime.TotalMinutes * Convert.ToDouble(60);
        }

        public static string GetTime(string strTime)
        {
            return strTime.Substring(0, 8).ToString();
        }


        #region ******************************验证手机号******************************
        public const string Mobile_ChinaUnicom_Pattern = @"(^130\d{8}$)|(^131\d{8}$)|(^132\d{8}$)|(^156\d{8}$)|(^185\d{8}$)|(^186\d{8}$)";
        /// <summary> 
        /// 验证手机号
        /// <para>中国联通手机号码</para>
        /// </summary>  
        /// <param name="source"></param> 
        /// <returns></returns>  
        public static bool IsChinaUnicomMobile(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return false;
            }
            return Regex.IsMatch(source, Mobile_ChinaUnicom_Pattern, RegexOptions.IgnoreCase);
        }
        #endregion ******************************验证手机号******************************


        /// <summary>
        /// 元素手势滑动方向
        /// </summary>
        public enum GestureDirection
        {
            None = 0,
            /// <summary>
            /// 从左往右移动
            /// </summary>
            FromLeftToRight = 10,
            /// <summary>
            /// 从右往左移动
            /// </summary>
            FromRightToLeft = 20,
            /// <summary>
            /// 从下往上移动
            /// </summary>
            FromDownToUp = 30,
            /// <summary>
            /// 从上往下移动
            /// </summary>
            FromUpToDown = 40
        }
    }

}
